﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Axe.Extensions;
using System.ComponentModel;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Drawing;
using System.Web.UI.HtmlControls;

[assembly: WebResource("Axe.Web.Controls.ColorPicker.js", "application/x-javascript")]

namespace Axe.Web.Controls
{
    public class ColorPickerItem : HardcodedDropDownList
    {
        #region public bool UseHexValues
        [DefaultValue(false)]
        public bool UseHexValues
        {
            get { return ViewState.Get("UseHexValues", false); }
            set { ViewState["UseHexValues"] = value; }
        } 
        #endregion

        public int SelectedColorValue
        {
            get
            {
                int value;
                if (Int32.TryParse(SelectedValue, out value))
                    return value;
                return 0;
            }
            set { SelectedValue = value.ToString(); }
        }

        protected override HardcodedDropDownListEventArgs OnRequestDataItems()
        {
            bool useHex = UseHexValues;
            string itemFormat = useHex ? "x2" : "";

            HardcodedDropDownListEventArgs args = new HardcodedDropDownListEventArgs();
            for (int i = 0; i < 255; i++)
            {
                string text;

                if (useHex)
                    text = i.ToString("x2");
                else
                    text = i.ToString();

                string value = i.ToString();

                ListItem item = new ListItem(text, value);
                args.BoundItems.Add(item);
            }

            return args;
        }
    }

    

   
    public class ColorPicker : CompositeControl, INamingContainer
    {
        ColorPickerItem rItem = new ColorPickerItem();
        ColorPickerItem gItem = new ColorPickerItem();
        ColorPickerItem bItem = new ColorPickerItem();
        HtmlGenericControl colorPreview = new HtmlGenericControl("span");

        #region public bool UseHexValues
        [DefaultValue(false)]
        public bool UseHexValues
        {
            get { return ViewState.Get("UseHexValues", false); }
            set { ViewState["UseHexValues"] = value; }
        }
        #endregion

        public Unit PreviewHeight
        {
            get { return ViewState.Get("PreviewHeight", new Unit(12,UnitType.Pixel)); }
            set { ViewState["PreviewHeight"] = value; }
        }

        public Unit PreviewWidth
        {
            get { return ViewState.Get("PreviewWidth", new Unit(12, UnitType.Pixel)); }
            set { ViewState["PreviewWidth"] = value; }
        }

        public Color SelectedColor
        {
            get
            {
                int r = rItem.SelectedColorValue;
                int g = gItem.SelectedColorValue;
                int b = bItem.SelectedColorValue;
                return Color.FromArgb(0, r, g, b);
            }
            set
            {
                rItem.SelectedColorValue = value.R;
                gItem.SelectedColorValue = value.G;
                bItem.SelectedColorValue = value.B;
            }
        }

        public ColorPicker()
        {
            rItem.ID = "Red";
            gItem.ID = "Green";
            bItem.ID = "Blue";

            colorPreview.Style["padding-top"] = PreviewHeight.ToString();
            colorPreview.Style["padding-bottom"] = "0";
            colorPreview.Style["padding-left"] = PreviewWidth.ToString();
            colorPreview.Style["padding-right"] = "0";
            colorPreview.ID = "Preview";
            colorPreview.Controls.Add(new LiteralControl("&nbsp;"));
        }

        protected override void CreateChildControls()
        {
            Controls.Clear();
            Controls.Add(rItem);
            Controls.Add(new LiteralControl("&nbsp;"));
            Controls.Add(gItem);
            Controls.Add(new LiteralControl("&nbsp;"));
            Controls.Add(bItem);
            Controls.Add(new LiteralControl("&nbsp;"));
            Controls.Add(colorPreview);
            colorPreview.Controls.Add(new LiteralControl("&nbsp;"));

            Page.ClientScript.RegisterClientScriptResource(typeof(ColorPicker), "Axe.Web.Controls.ColorPicker.js");
            ChildControlsCreated = true;
            ClearChildViewState();
        }

        protected override void OnPreRender(EventArgs e)
        {
            rItem.UseHexValues = UseHexValues;
            gItem.UseHexValues = UseHexValues;
            bItem.UseHexValues = UseHexValues;


            colorPreview.Style["background"] = ColorTranslator.ToHtml(SelectedColor);

            string script = String.Format("return UpdateColorPicker($('{0}'),$('{1}'),$('{2}'),$('{3}'));",
                rItem.ClientID,
                gItem.ClientID,
                bItem.ClientID,
                colorPreview.ClientID);

            rItem.Attributes["onchange"] = script;
            gItem.Attributes["onchange"] = script;
            bItem.Attributes["onchange"] = script;

            base.OnPreRender(e);
        }
    }
}
